package com.hustbaidu.hustmap;

import java.net.Socket;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ScoreManager {
	private static HustMapDBHelper dbHelper;
	private static SQLiteDatabase db;

	public ScoreManager(Context context){
		dbHelper = new HustMapDBHelper(context.getApplicationContext());
		db = dbHelper.getReadableDatabase();
	}
	
	public static void init(Context context){
		dbHelper = new HustMapDBHelper(context.getApplicationContext());
		try {
			db = dbHelper.getWritableDatabase();
		} catch (Exception e) {
			// TODO: handle exception
			dbHelper.close();
			db = dbHelper.getWritableDatabase();
		}
		
	}
	
	/**
	 * 获取所有的点评信息
	 * @return 所有的点评信息的列表
	 */
	public static List<Score> getAllScores(){
		List<Score> list = new ArrayList<Score>();
		Cursor cursor = db.query("score", null, null, null, null, null, null);
		Score score = null;
		while(cursor.moveToNext()){
			score = new Score();
			score.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			score.setUserName(cursor.getString(cursor.getColumnIndex("user_name")));
			score.setContent(cursor.getString(cursor.getColumnIndex("content")));
			
			String time = cursor.getString(cursor.getColumnIndex("time"));
			try{
				score.setTime(Timestamp.valueOf(time));
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			
			score.setPlace(cursor.getInt(cursor.getColumnIndex("place")));
			score.setScore(cursor.getInt(cursor.getColumnIndex("score")));
			
			list.add(score);
			
		}
		cursor.close();
		return list;
	}
	
	/**
	 * 根据地点的ID获取对应地点的点评信息
	 * @param placeId 地点的ID
	 * @return 对应地点的点评信息列表
	 */
	public static List<Score> getAllScores(int placeId){
		List<Score> list = new ArrayList<Score>();
		Cursor cursor = db.query("score", null, "place = ?", new String[]{String.valueOf(placeId)}, null, null, null);
		Score score = null;
		while(cursor.moveToNext()){
			score = new Score();
			score.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			score.setUserName(cursor.getString(cursor.getColumnIndex("user_name")));
			score.setContent(cursor.getString(cursor.getColumnIndex("content")));
			
			String time = cursor.getString(cursor.getColumnIndex("time"));
			try{
				score.setTime(Timestamp.valueOf(time));
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			
			score.setPlace(cursor.getInt(cursor.getColumnIndex("place")));
			score.setScore(cursor.getInt(cursor.getColumnIndex("score")));
			
			list.add(score);
			
		}
		cursor.close();
		return list;
	}
	
	/**
	 * 根据地点的ID获取对应地点的点评数量
	 * @param placeId 地点的ID
	 * @return 对应地点的点评数量
	 */
	public static int getScoreNumberByPlace(int placeId){
		Cursor cursor = db.query("score", null, "place = ?", new String[]{String.valueOf(placeId)}, null, null, null);
		int scoreNum = cursor.getCount();
		cursor.close();
		return scoreNum;
	}
	
	/**
	 * 根据地点的ID获取对应地点的平均分
	 * @param placeId 地点的ID
	 * @return 对应地点的平均分
	 */
	public static float getAverageScoreByPlace(int placeId){
		Cursor cursor = db.query("score", new String[]{"score"}, "place = ?", new String[]{String.valueOf(placeId)}, null, null, null);
		int sum = 0;
		int num = 0;
		while(cursor.moveToNext()){
			sum += cursor.getInt(cursor.getColumnIndex("score"));
			num++;
		}
		cursor.close();
		return ((float)(sum * 10 / num)) / 10 ;
	}

}
